package com.gamota.mobi.adapter;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.gamota.mobi.ItemBrokerActivity;
import com.gamota.mobi.NotificationMessageActivity;
import com.gamota.mobi.R;
import com.gamota.mobi.util.Constants;
import com.gamota.mobi.util.Utils;

public class NotificationHelper {

	private NotificationManager notifyManager;
    private NotificationCompat.Builder notifyBuilder;
    private Context context;
    private Bitmap largeIcon;
    private Bitmap giftIcon;
    private String countryCode;
    
    public NotificationHelper(Context context){
    	 this.context = context;
    	 notifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
         notifyBuilder = new NotificationCompat.Builder(context);
         largeIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
         countryCode = Utils.getStaticCountryCode(context);
    }
    
    public void createNotification(int id, String title, String content, String ticker, PendingIntent pending){
    	notifyBuilder.setOngoing(true);
        notifyBuilder.setTicker(ticker + " \"" + title + "\"");
        notifyBuilder.setSmallIcon(android.R.drawable.stat_sys_download);
        notifyBuilder.setLargeIcon(largeIcon);
	    notifyBuilder.setContentIntent(pending);
    	notifyBuilder.setContentText(content);
    	notifyBuilder.setContentTitle(title);
        notifyManager.notify(id, notifyBuilder.build());
    }
    
    public void cancel(int id){
    	notifyManager.cancel(id);
    }
    
    public void updateProgress(int id, String title){
    	notifyBuilder.setContentText(context.getResources().getString(R.string.downloading));
    	notifyBuilder.setLargeIcon(largeIcon);
    	notifyBuilder.setContentTitle(title);
        notifyManager.notify(id, notifyBuilder.build());
    }
    
    public void pauseNotification(int id, String title){
    	notifyBuilder.setContentText(context.getResources().getString(R.string.paused));
    	notifyBuilder.setLargeIcon(largeIcon);
    	notifyBuilder.setContentTitle(title);
        notifyManager.notify(id, notifyBuilder.build());
    }
    
    public void complete(int id, Bitmap largeIcon, String title, String content, String ticker, PendingIntent intent){
    	if(null == largeIcon){
    		largeIcon = this.largeIcon;
    	}
    	notifyBuilder.setOngoing(false);
        notifyBuilder.setSmallIcon(android.R.drawable.stat_sys_download_done);
        notifyBuilder.setLargeIcon(largeIcon);
        notifyBuilder.setTicker(ticker + " \"" + title + "\"");
        notifyBuilder.setAutoCancel(true);
	    notifyBuilder.setContentIntent(intent);
    	notifyBuilder.setContentText(content);
    	notifyBuilder.setContentTitle(title);
        notifyManager.notify(id, notifyBuilder.build());
    }
    
    public void createPushNotification(Bundle data){
    	if(!countryCode.equalsIgnoreCase("vn")){
    		return;
    	}
    	String title = data.getString("title");
        String message = data.getString("message");
        String link = data.getString("link_url");
        String store = data.getString("store");
        String contentId = data.getString("content_id");
        Intent notificationIntent = new Intent();
        if(message.contains("GCM") || message.contains("Trying (attempt")){
        	return;
        }
        if(!link.equals("")){
        	 notificationIntent = new Intent(Intent.ACTION_VIEW);
        	 notificationIntent.setData(Uri.parse(link));
        	 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        if(!store.equals("") && !contentId.equals("")){
        	System.err.println("push store " + store);
        	if(store.equalsIgnoreCase("android")){
        		store = "apps";
        	} else if(store.equalsIgnoreCase("ebook")){
        		store = "books";
        	} else if(store.equalsIgnoreCase("comic")){
        		store = "comics";
        	} else {
        		store = "movies";
        	}
        	notificationIntent = new Intent(context, ItemBrokerActivity.class);
        	notificationIntent.putExtra(Constants.SLUG, contentId);
        	notificationIntent.putExtra(Constants.PREVIOUS_SCREEN, "/Notification");
        	notificationIntent.putExtra(Constants.KEY_REFERER, "push");
        	notificationIntent.putExtra(Constants.STORE_KEY, store);
        	notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        if(link.equals("") && store.equals("")){
        	notificationIntent = new Intent(context, NotificationMessageActivity.class);
        	notificationIntent.putExtra(Constants.MESSAGE, message);
        	notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
    	notifyBuilder.setAutoCancel(true);
    	notifyBuilder.setOngoing(false);
        notifyBuilder.setTicker(title);
        notifyBuilder.setWhen(System.currentTimeMillis());
        notifyBuilder.setLargeIcon(largeIcon);
        notifyBuilder.setSmallIcon(R.drawable.ic_notification);
	    PendingIntent pending = PendingIntent.getActivity(context, 0, notificationIntent, 0);
	    notifyBuilder.setContentIntent(pending);
    	notifyBuilder.setContentText(message);
    	notifyBuilder.setContentTitle(context.getResources().getString(R.string.app_name));
    	notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
        notifyManager.notify(303, notifyBuilder.build());
    }
    
    public void createGiftNotification(String title, String ticker, PendingIntent pending, String content, int id){
    	if(!countryCode.equalsIgnoreCase("vn")){
    		return;
    	}
    	giftIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
    	notifyBuilder.setOngoing(true);
    	notifyBuilder.setAutoCancel(true);
        notifyBuilder.setTicker(ticker + " \"" + title + "\"");
        notifyBuilder.setSmallIcon(R.drawable.ic_notification);
        notifyBuilder.setLargeIcon(giftIcon);
	    notifyBuilder.setContentIntent(pending);
    	notifyBuilder.setContentText(content);
    	notifyBuilder.setContentTitle(title);
    	notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
        notifyManager.notify(id, notifyBuilder.build());
    }
    
    public void createContentUpdateNotification(int id, String title, int updateNumber, String content, PendingIntent pending){
    	if(!countryCode.equalsIgnoreCase("vn")){
    		return;
    	}
    	notifyBuilder.setOngoing(false);
    	notifyBuilder.setAutoCancel(true);
        notifyBuilder.setSmallIcon(R.drawable.ic_notification);
        notifyBuilder.setLargeIcon(largeIcon);
	    notifyBuilder.setContentIntent(pending);
    	notifyBuilder.setContentText(content);
    	notifyBuilder.setContentTitle(String.format(title, updateNumber));
        notifyManager.notify(id, notifyBuilder.build());
    }
}
